var sources = require('./../lib/source');
var getitem = require('./../lib/sourceitem')(sources);
module.exports = function (UC, source, config, ArFor, cache, package) {
    var UserControl = UC;
    var router = function (req, res, next) {
        var login = false;
        if (UserControl.getoff(req.sessionID, 1)) {
            login = true;
        }
        new source(getitem('W_21'), req.params, function () {
            if (req.UserAgent.isMobile) {
                res.redirect('./../../../../mobile/#power=' + req.params.category_id + "&id=" + req.params.id);
            } else {

                
                var manifest_id = null;
                
                var File = null;
 
                        new source(getitem('W_26'), {}, function (website) {
                        var category = [{
                            title: '首页',
                            id: 'Home',
                            active: true
                        }];
                        new source(getitem("W_10"), {
                            'website': '1'
                        }, function (db) {
                            if (db) {
                                db = db.Table;
                                db = db.reverse();
                                db.push(category[0]);
                                category = db.reverse();
                            }
                            if (req.params.category_id) {
                                for (var i = 0; i < category.length; i++) {
                                    if (req.params.category_id == category[i].id) {
                                        delete category[0].active;
                                        category[i].active = true;
                                    }
                                }
                            }
                            if (category[0].active) {
                                res.redirect('./../../../');
                            } else {
                                var dblist = {
                                    CarouselList: {
                                        source_id: getitem("W_18"),
                                        Request: {},
                                        db: []
                                    },
                                    NoticeBoard: {
                                        source_id: getitem("W_19"),
                                        Request: {},
                                        db: []
                                    },
                                    ImageList: {
                                        source_id: getitem("W_20"),
                                        Request: {},
                                        db: []
                                    },
                                    info: {
                                        source_id: getitem("W_9"),
                                        Request: req.params,
                                        db: {}
                                    },
                                    Hotspot: {
                                        source_id: getitem("W_13"),
                                        Request: {},
                                        db: []
                                    }
                                };
                                new ArFor(dblist, function () { 
                                    cache.render({
                                        id: package.id,
                                        file: './jview/index'
                                    }, {
                                        title:website.name,
                                        icon:website.icon,
                                        style:website.style,
                                        manifest_id: manifest_id,
                                        login: login,
                                        sys_id: package.id,
                                        category: category,
                                        UserAgent: req.UserAgent,
                                        Hotspot: dblist.Hotspot.db,
                                        info: dblist.info.db,
                                        CarouselList: dblist.CarouselList.db,
                                        NoticeBoard: dblist.NoticeBoard.db,
                                        ImageList: dblist.ImageList.db,
                                        PageType: 'content'
                                    }, req, res, File);
                                }).forEach(function (item, __next, key) {
                                    if (item && item.source_id) {
                                        new source(item.source_id, item.Request, function (_db) {
                                            if (_db && _db.Table) {
                                                item.db = _db.Table;
                                            } else if (_db && key == 'info') {
                                                item.db = _db;
                                            } else {

                                            }
                                            __next()
                                        })
                                    } else {
                                        __next()
                                    }
                                })
                            }
                        })
                    })
                     
            }
        })
    }
    return router;
};